package com.getpebble.android.framework.util;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import com.getpebble.android.common.core.trace.Trace;
import com.sun.mail.imap.protocol.ListInfo;
import com.sun.mail.smtp.SMTPMessage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.io.IOUtils;

public class SideloadingUtils {
    private static final String TAG = SideloadingUtils.class.getSimpleName();

    static /* synthetic */ class AnonymousClass1 {
        static final /* synthetic */ int[] $SwitchMap$com$getpebble$android$framework$util$SideloadingUtils$BundleType = new int[BundleType.values().length];

        static {
            try {
                $SwitchMap$com$getpebble$android$framework$util$SideloadingUtils$BundleType[BundleType.APP.ordinal()] = 1;
            } catch (NoSuchFieldError e) {
            }
            try {
                $SwitchMap$com$getpebble$android$framework$util$SideloadingUtils$BundleType[BundleType.FIRMWARE.ordinal()] = 2;
            } catch (NoSuchFieldError e2) {
            }
            try {
                $SwitchMap$com$getpebble$android$framework$util$SideloadingUtils$BundleType[BundleType.LANGUAGE.ordinal()] = 3;
            } catch (NoSuchFieldError e3) {
            }
        }
    }

    public enum BundleType {
        APP,
        FIRMWARE,
        LANGUAGE
    }

    public static File downloadContentUriToFile(Context context, Uri uri, BundleType type) {
        IOException e;
        OutputStream outputStream;
        FileNotFoundException fnfe;
        Throwable th;
        FileOutputStream fOut = null;
        InputStream is = null;
        File outputFile = null;
        try {
            is = context.getContentResolver().openInputStream(uri);
            File outputDir = context.getCacheDir();
            switch (AnonymousClass1.$SwitchMap$com$getpebble$android$framework$util$SideloadingUtils$BundleType[type.ordinal()]) {
                case SMTPMessage.RETURN_FULL /*1*/:
                    outputFile = File.createTempFile("tmp_app_bundle_localdl", ".pbw", outputDir);
                    break;
                case SMTPMessage.RETURN_HDRS /*2*/:
                    outputFile = File.createTempFile("tmp_firmware", ".pbz", outputDir);
                    break;
                case ListInfo.INDETERMINATE /*3*/:
                    outputFile = File.createTempFile("tmp_lang_bundle_localdl", ".pbl", outputDir);
                    break;
            }
            OutputStream fOut2 = new FileOutputStream(outputFile);
            try {
                IOUtils.copy(is, fOut2);
                if (fOut2 != null) {
                    try {
                        fOut2.flush();
                        fOut2.close();
                    } catch (IOException e2) {
                        Trace.warning(TAG, "ioe", e2);
                    }
                    if (is != null) {
                        try {
                            is.close();
                        } catch (IOException e22) {
                            Trace.warning(TAG, "ioe", e22);
                            outputStream = fOut2;
                        }
                    }
                    outputStream = fOut2;
                }
            } catch (FileNotFoundException e3) {
                fnfe = e3;
                outputStream = fOut2;
                try {
                    Trace.warning(TAG, "Could not download bundle cause couldn't find=" + outputFile, fnfe);
                    if (fOut != null) {
                        try {
                            fOut.flush();
                            fOut.close();
                        } catch (IOException e222) {
                            Trace.warning(TAG, "ioe", e222);
                        }
                        if (is != null) {
                            try {
                                is.close();
                            } catch (IOException e2222) {
                                Trace.warning(TAG, "ioe", e2222);
                            }
                        }
                    }
                    if (outputFile != null) {
                        return outputFile;
                    }
                    Trace.debug(TAG, "outputFile == null");
                    return null;
                } catch (Throwable th2) {
                    th = th2;
                    if (fOut != null) {
                        try {
                            fOut.flush();
                            fOut.close();
                        } catch (IOException e22222) {
                            Trace.warning(TAG, "ioe", e22222);
                        }
                        if (is != null) {
                            try {
                                is.close();
                            } catch (IOException e222222) {
                                Trace.warning(TAG, "ioe", e222222);
                            }
                        }
                    }
                    throw th;
                }
            } catch (IOException e4) {
                e222222 = e4;
                outputStream = fOut2;
                Trace.warning(TAG, "Could not download bundle IO issue.", e222222);
                if (fOut != null) {
                    try {
                        fOut.flush();
                        fOut.close();
                    } catch (IOException e2222222) {
                        Trace.warning(TAG, "ioe", e2222222);
                    }
                    if (is != null) {
                        try {
                            is.close();
                        } catch (IOException e22222222) {
                            Trace.warning(TAG, "ioe", e22222222);
                        }
                    }
                }
                if (outputFile != null) {
                    return outputFile;
                }
                Trace.debug(TAG, "outputFile == null");
                return null;
            } catch (Throwable th3) {
                th = th3;
                outputStream = fOut2;
                if (fOut != null) {
                    fOut.flush();
                    fOut.close();
                    if (is != null) {
                        is.close();
                    }
                }
                throw th;
            }
        } catch (FileNotFoundException e5) {
            fnfe = e5;
            Trace.warning(TAG, "Could not download bundle cause couldn't find=" + outputFile, fnfe);
            if (fOut != null) {
                fOut.flush();
                fOut.close();
                if (is != null) {
                    is.close();
                }
            }
            if (outputFile != null) {
                return outputFile;
            }
            Trace.debug(TAG, "outputFile == null");
            return null;
        } catch (IOException e6) {
            e22222222 = e6;
            Trace.warning(TAG, "Could not download bundle IO issue.", e22222222);
            if (fOut != null) {
                fOut.flush();
                fOut.close();
                if (is != null) {
                    is.close();
                }
            }
            if (outputFile != null) {
                return outputFile;
            }
            Trace.debug(TAG, "outputFile == null");
            return null;
        }
        if (outputFile != null) {
            return outputFile;
        }
        Trace.debug(TAG, "outputFile == null");
        return null;
    }

    public static boolean isContentScheme(Uri uri) {
        return uri.getScheme().equals("content");
    }

    public static String getContentName(ContentResolver resolver, Uri uri) {
        Cursor cursor = resolver.query(uri, new String[]{"_display_name"}, null, null, null);
        if (cursor == null) {
            return null;
        }
        String name = null;
        cursor.moveToFirst();
        if (!cursor.isAfterLast()) {
            int nameIndex = cursor.getColumnIndex("_display_name");
            if (nameIndex >= 0) {
                name = cursor.getString(nameIndex);
            }
        }
        cursor.close();
        return name;
    }
}
